<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/4
 * Time: 上午10:20
 */

namespace App\Modules\Finance\Services;


use App\Modules\Finance\Models\Bank;
use App\Modules\Finance\Models\CashAccount;

class CashAccountService
{

    /**
     * 获取渠道银行账号信息
     * @param $channelId
     * @return mixed
     */
    public static function getCashAccountSingle($channelId) {
        $cashAccount = CashAccount::getByDistributionChannel($channelId);
        return $cashAccount;
    }

    /**
     * 添加银行账号
     * @param $channelId
     * @param $account_name
     * @param $identity_card
     * @param $card_number
     * @param $account_bank 开户支行
     * @param $bank_id 交易银行ID
     * @param $bank 交易银行
     * @param $province
     * @param $is_company 0:私人； 1:公司
     * @param String $phone 电话号码
     * @return mixed
     */
    public static function addCashAccount($channelId, $account_name, $identity_card, $card_number, $account_bank, $bank_id, $bank, $province = '', $is_company = 0, $phone = '') {
        $bankD = BankService::getBankSingle($bank_id);

        if($is_company == 0 || $is_company == 1) {
        } else {
            $is_company = 0;
        }

        $data['distribution_channel_id'] = $channelId;
        $data['account_name'] = trim($account_name);
        $data['identity_card'] = trim($identity_card);
        $data['card_number'] = trim($card_number);
        $data['account_bank'] = trim($account_bank);
        $data['bank'] = trim($bank);
        $data['bank_id'] = $bank_id;
        $data['province'] = trim($province);
        $data['status'] = -1;
        $data['is_company'] = $is_company;
        $data['phone'] = trim($phone);

        $cashAccount = CashAccount::create($data);
        return $cashAccount;
    }

    /**
     * 账号是否可以编辑
     * @param $channelId
     * @return bool true：可以编辑    false：不可以编辑
     */
    public static function isEditCashAccount($channelId) {
        $cashAccount = self::getCashAccountSingle($channelId);
        if(!empty($cashAccount) && $cashAccount['status'] == -1) {
            return false;
        }
        return true;
    }

    /**
     * 更新账号信息
     * @param $channelId
     * @param string $account_name
     * @param string $identity_card
     * @param string $card_number
     * @param string $account_bank
     * @param string $bank_id 交易银行ID
     * @param string $bank
     * @param string $province
     * @param string $is_company 0:私人； 1:公司
     * @param String $phone 电话号码
     * @return mixed|void
     */
    public static function updateCashAccount($channelId, $account_name='', $identity_card='', $card_number='', $account_bank='', $bank_id = '', $bank='', $province='', $is_company = '', $phone = '') {
        $cashAccount = self::getCashAccountSingle($channelId);
        if(empty($cashAccount)) {
            return '';
        }

        if($is_company == 0 || $is_company == 1) {
        } else {
            $is_company = '';
        }

        if($channelId) {
            $cashAccount['distribution_channel_id'] = $channelId;
        }
        if($account_name) {
            $cashAccount['account_name'] = $account_name;
        }
        if($identity_card) {
            $cashAccount['identity_card'] = $identity_card;
        }
        if($card_number) {
            $cashAccount['card_number'] = $card_number;
        }
        if($account_bank) {
            $cashAccount['account_bank'] = $account_bank;
        }
        if($bank_id) {
            $cashAccount['bank_id'] = $bank_id;
        }
        if($bank) {
            $cashAccount['bank'] = $bank;
        }
        if($province) {
            $cashAccount['province'] = $province;
        }
        if(is_numeric($is_company) && in_array($is_company, [0, 1])) {
            $cashAccount['is_company'] = $is_company;
        }
        if($phone) {
            $cashAccount['phone'] = $phone;
        }
        $cashAccount['status'] = -1;

        $cashAccount->save();

        return $cashAccount;
    }

    /**
     * 更新账号可编辑修改
     * @param $channelId
     * @return mixed|void
     */
    public static function updateCashAccountEdit($channelId) {
        $cashAccount = self::getCashAccountSingle($channelId);
        if(empty($cashAccount)) {
            return;
        }
        $cashAccount['status'] = 0;

        $cashAccount->save();

        return $cashAccount;
    }

    /**
     * 渠道账户是否设置
     * @param $channelId
     * @return bool true:已经设置   false:没有设置
     */
    public static function isCashAccountExits($channelId) {
        $cashAccount = self::getCashAccountSingle($channelId);
        if(empty($cashAccount)) {
            return false;
        }
        if(empty($cashAccount['account_name'])
            || empty($cashAccount['card_number'])
            || empty($cashAccount['bank'])) {
            return false;
        }
//        if(empty($cashAccount['account_name'])
//            || empty($cashAccount['identity_card'])
//            || empty($cashAccount['card_number'])
//            || empty($cashAccount['account_bank'])
//            || empty($cashAccount['bank'])) {
//            return false;
//        }
        return true;
    }
}